package me.zhengjie.modules.app.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.app.domain.UserMember;
import me.zhengjie.modules.app.domain.dto.UserMemberDto;
import me.zhengjie.modules.app.domain.vo.UserMemberVo;
import me.zhengjie.modules.app.mapstruct.UserMemberMapStruct;
import me.zhengjie.modules.app.service.UserMemberService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* @author xz
* @date 2025/06/23
*/
@Slf4j
@Api("app/api/v1/user-member")
@RestController
@RequestMapping("/app/api/v1/user-member")
public class UserMemberController {
    @Resource
    private UserMemberService userMemberService;
    @Resource
    private UserMemberMapStruct userMembermapStruct;

    @ApiOperation("新增")
    @PostMapping("/add")
    public UserMemberDto add(@RequestBody UserMemberDto userMemberDto) {
        UserMember dataModel=userMembermapStruct.fromDto2Model(userMemberDto);
        userMemberService.insertData(dataModel);
        return userMembermapStruct.fromModel2Dto(dataModel);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable("id") String id) {
        return userMemberService.removeById(id);
    }

    @ApiOperation("修改")
    @PutMapping("/update/{id}")
    public UserMemberDto update(@PathVariable("id") String id, @RequestBody UserMemberDto userMemberDto) {
        if (null == userMemberDto.getId()) {
        			userMemberDto.setId(id);
        }
        UserMember updateDataModel=userMembermapStruct.fromDto2Model(userMemberDto);
        userMemberService.saveOrUpdate(updateDataModel);
        return userMembermapStruct.fromModel2Dto(updateDataModel);
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/findById/{id}")
    public UserMemberDto detail(@PathVariable("id") String id) {
        UserMember userMember = userMemberService.getById(id);
        return userMembermapStruct.fromModel2Dto(userMember);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    public Page<UserMemberDto> page(@RequestBody UserMemberVo userMemberVo) {
        Page<UserMember> page = new Page<>(userMemberVo.getPageNo(), userMemberVo.getPageSize());
        Page pageInfo = userMemberService.page(page);

        List<UserMemberDto> resList=userMembermapStruct.fromModel2Dtos(page.getRecords());
        pageInfo.setRecords(resList);
        return pageInfo;
    }
}
